package toOffer;

/**
 * <h3>leetcode</h3>
 *
 * @author BigFaceCat
 * @date 2024/3/30 19:25
 * @description <p>滑动窗口的平均值</p>
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 题目：请实现如下类型 MovingAverage，计算滑动窗口中所有数字的平
 * 均值，该类型构造函数的参数确定滑动窗口的大小，每次调用成员函数 next
 * 时都会在滑动窗口中添加一个整数，并返回滑动窗口中所有数字的平均值。
 */
public class toOffer7_2_41 {

    private final int[] arr = new int[100000];
    private Integer num = 0;
    private int left = 0;
    private int right = left + num;

    public toOffer7_2_41(int size) {
        this.num = size;
    }

    public double next(int val) {
        double avg = 0;

        if (right - left < num){
            right++;
        }else {
            right++;
            left++;
        }
        arr[right - 1] = val;

        for (int i = left; i < right; i++) {
            avg = avg + arr[i];
        }

        return avg / (right - left);
    }

    /**
     * 思路：
     * 蠢比题目，题目明显要求的是用队列来做，fifo。但是可以用一个很长的数组来模拟队列，不用管进出，直接双指针模拟窗口就行
     *
     * @param args
     */
    public static void main(String[] args) {
        String[] inputs01 = {"MovingAverage", "next", "next", "next", "next"};
        int[][] inputs02 = {{3}, {1}, {10}, {3}, {5}};

        toOffer7_2_41 movingAverage = new toOffer7_2_41(inputs02[0][0]);
        for (int i = 1; i < inputs02.length; i++) {
            System.out.println(movingAverage.next(inputs02[i][0]));
        }
    }


}
